/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rcfi.lankaweb.linklk.control;

/**
 *
 * @author priyantha
 */
import com.rcfi.lankaweb.linklk.model.user;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import java.util.HashMap;
import java.util.Map;
import com.rcfi.lankaweb.linklk.domain.country;
import com.rcfi.lankaweb.linklk.domain.countrycode;
import com.rcfi.lankaweb.linklk.model.user;
import com.rcfi.lankaweb.linklk.service.*;
import javax.servlet.http.HttpServletRequest;
import com.rcfi.lankaweb.linklk.validate.userValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.rcfi.lankaweb.linklk.model.user;
import java.util.List;
import javax.validation.Valid;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/enterdetails")
@SessionAttributes("user")
public class userController {

    private userService userService;
    private countryCodeService countryCodeService;
    private userValidator userValidator;

    @Autowired
    public userController(userService userService, userValidator userValidator, countryCodeService countryCodeService) {

        this.userService = userService;
        this.userValidator = userValidator;
        this.countryCodeService = countryCodeService;
    }

    public void setCountryCodeService(countryCodeService countryCodeService) {
        this.countryCodeService = countryCodeService;
    }

    public void setUserService(userService userService) {
        this.userService = userService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String simpleForm(Model model) {
        List<country> countryList = userService.getAllCountries();
        model.addAttribute("countryList", countryList);

        List<countrycode> countryCodeList = countryCodeService.getAllCodes();
        model.addAttribute("countryCodeList", countryCodeList);

        model.addAttribute(new user());
        return "enterdetails";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String simple(@ModelAttribute("user") @Valid user user, BindingResult result, ModelMap model) {

        if (result.hasErrors()) {

            return "enterdetails";

        }
        model.addAttribute("user", user);
        model.put("enterdetails", user);
        return "viewdetails";

    }
}

